package com.iwaster.web.servlet;

import com.iwaster.domain.PageBean;
import com.iwaster.domain.Student;
import com.iwaster.service.StudentService;
import com.iwaster.service.impl.StudentServiceImpl;
import com.iwaster.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * wgy 2019/4/23 14:35
 */
@WebServlet(name = "StudentListServlet",urlPatterns = "/stulist")
public class StudentListServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        
        //1获取pageIndex,pageSize;
        String spageIndex = request.getParameter("pageIndex");
        String spageSize = request.getParameter("pageSize");
        int pageIndex=1;
        int pageSize=10;
        if(!StringUtils.isEmpty(spageIndex)){
            pageIndex=Integer.parseInt(spageIndex);
        }

        if(!StringUtils.isEmpty(spageSize)){
            pageSize=Integer.parseInt(spageSize);
        }
        //分页查询
        StudentService studentService=new StudentServiceImpl();
        PageBean<Student> pageBean=studentService.getDataByPage(pageIndex,pageSize);
        if(pageBean!=null){
            request.setAttribute("pageBean", pageBean);
        }else{
            request.setAttribute("msg", "数据为空");
        }
        request.getRequestDispatcher("/studentlist.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
